package com.taoyibao.hongcanting.base.delegate;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kymjs.frame.presenter.ActivityPresenter;
import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.ui.HongApplication;
import com.taoyibao.hongcanting.utils.UltimateBarUtils;

/**
 * 有toolbar的页面继承该类
 */
public abstract class ToolbarDelegate extends BaseDelegate {
    private Toolbar mToolbar;
    private View divider;

    @Override
    public void create(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.create(inflater, container, savedInstanceState);
        LinearLayout toolbarRootView = new LinearLayout(getActivity());

        ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);//定义布局管理器的参数
        toolbarRootView.setOrientation(LinearLayout.VERTICAL);
        toolbarRootView.setLayoutParams(param);
        mToolbar = (Toolbar) View.inflate(getActivity(), R.layout.base_toolbar, null);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            mToolbar.setPadding(0, 0, 0, 0);
        } else {
            int height = UltimateBarUtils.getStatusBarHeight(HongApplication.getContext());
            mToolbar.setPadding(0, height, 0, 0);
        }
        mToolbar.setTitle("");
        ((ActivityPresenter) getActivity()).setSupportActionBar(mToolbar);

        divider = new View(this.getActivity());
        divider.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
        divider.setBackgroundColor(getActivity().getResources().getColor(R.color.colorTitleLine));

        toolbarRootView.addView(mToolbar);
        toolbarRootView.addView(divider);
        toolbarRootView.addView(rootView);
        rootView = toolbarRootView;
    }

    @Override
    public void initWidget() {
        super.initWidget();
    }

    @Override
    public Toolbar getToolbar() {
        return mToolbar;
    }

    @Override
    public <T extends View> T bindView(int id) {
        T view = (T) mViews.get(id);
        if (view == null) {
            view = (T) rootView.findViewById(id);
            if (view == null)
                view = (T) getToolbar().findViewById(id);
            mViews.put(id, view);
        }
        return view;
    }

    /**
     * 设置页面顶部的title
     *
     * @param title
     */
    public void setTitle(String title) {
        TextView textView = get(R.id.tv_toolbar_title);
        textView.setText(title);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnTitleCliclListener != null) {
                    mOnTitleCliclListener.onTitleClick(v);
                }
            }
        });
    }

    /**
     * 设置title的字体颜色
     *
     * @param color
     */
    public void setTitleColor(int color) {
        TextView textView = get(R.id.tv_toolbar_title);
        textView.setTextColor(color);
    }

    /**
     * 设置隐藏title
     */
    public void setHideTitle() {
        get(R.id.tv_toolbar_title).setVisibility(View.GONE);
    }


    /**
     * 显示返回箭头 并设置默认的点击事件是关闭页面
     */
    public void setBackHeadGray() {
        ImageView imageView = get(R.id.iv_toolbar_left);
        get(R.id.iv_toolbar_left).setVisibility(View.VISIBLE);
        imageView.setImageResource(R.drawable.back_lift_default);
        get(R.id.layout_toolbar_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnLeftCilckListener != null) {
                    mOnLeftCilckListener.onLeftClick(v);
                } else {
                    getActivity().finish();
                }
            }
        });
    }

    /**
     * 设置左侧的图片
     *
     * @param imgId
     */
    public void setLeftImg(int imgId) {
        ImageView imageView = get(R.id.iv_toolbar_left);
        imageView.setImageResource(imgId);
        get(R.id.layout_toolbar_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnLeftCilckListener != null) {
                    mOnLeftCilckListener.onLeftClick(v);
                } else {
                    getActivity().finish();
                }
            }
        });
    }

    /**
     * 设置左侧的文字
     *
     * @param text
     */
    public void setLeftText(String text) {
        TextView textView = get(R.id.tv_toolbar_left);
        textView.setVisibility(View.VISIBLE);
        textView.setText(text);
        get(R.id.layout_toolbar_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnLeftCilckListener != null) {
                    mOnLeftCilckListener.onLeftClick(v);
                } else {
                    getActivity().finish();
                }
            }
        });
    }

    /**
     * 隐藏返回键
     */
    public void setHideLeftBack() {
        get(R.id.layout_toolbar_left).setVisibility(View.GONE);
    }

    /**
     * 设置右侧显示的文字
     *
     * @param text
     */
    public void setRightText(String text) {
        TextView textView = get(R.id.tv_toolbar_right);
        textView.setVisibility(View.VISIBLE);
        textView.setText(text);
        get(R.id.layout_toolbar_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRightClickListener != null) {
                    mOnRightClickListener.onRightClick(v);
                }
            }
        });
    }

    /**
     * 设置右侧显示图片
     *
     * @param id
     */
    public void setRightImg(int id) {
        ImageView imageView = get(R.id.iv_toolbar_right);
        imageView.setVisibility(View.VISIBLE);
        imageView.setImageResource(id);
        get(R.id.layout_toolbar_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRightClickListener != null) {
                    mOnRightClickListener.onRightClick(v);
                }
            }
        });
    }

    /**
     * @param color
     */
    public void setRightTextColor(int color) {
        TextView textView = get(R.id.tv_toolbar_right);
        textView.setVisibility(View.VISIBLE);
        textView.setTextColor(color);
    }

    /**
     * 设置隐藏右侧所有内容
     *
     * @param b true 隐藏 false 显示
     */
    public void setHideRightAll(boolean b) {
        get(R.id.layout_toolbar_rightAll).setVisibility(b ? View.GONE : View.VISIBLE);
    }

    /**
     * 右侧只有图片时使用
     *
     * @param imgId
     */
    public void setRightOnlyImg(int imgId) {
        final ImageView imageView = get(R.id.iv_toolbar_onlyRight);
        imageView.setImageResource(imgId);
        imageView.setVisibility(View.VISIBLE);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnRightClickListener != null) {
                    mOnRightClickListener.onRightClick(imageView);
                }
            }
        });
    }

    // 隐藏分割线
    public void setHideDivder() {
        divider.setVisibility(View.GONE);
    }

    private OnRightClickListener mOnRightClickListener;

    public void setOnRightClickListener(OnRightClickListener onRightClickListener) {
        mOnRightClickListener = onRightClickListener;
    }

    public interface OnRightClickListener {
        /**
         * 右侧的点击事件
         *
         * @param view
         */
        void onRightClick(View view);
    }

    public void setOnLeftCilckListener(OnLeftCilckListener onLeftCilckListener) {
        mOnLeftCilckListener = onLeftCilckListener;
    }

    private OnLeftCilckListener mOnLeftCilckListener;

    public interface OnLeftCilckListener {
        /**
         * 左侧返回按钮ImageView的点击事件
         *
         * @param view
         */
        void onLeftClick(View view);
    }

    private OnTitleCliclListener mOnTitleCliclListener;

    public void setOnTitleCliclListener(OnTitleCliclListener onTitleCliclListener) {
        mOnTitleCliclListener = onTitleCliclListener;
    }

    public interface OnTitleCliclListener {
        /**
         * title的点击事件
         *
         * @param view
         */
        void onTitleClick(View view);
    }

}
